package org.dizena.common.utils;

import com.alibaba.fastjson2.JSON;
import org.dizena.common.base.enums.EnumLogLevel;
import org.dizena.common.logger.bean.LogInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtil {

    public static void error(Object source, Throwable e) {
        String message = e.getMessage();
        for (StackTraceElement element : e.getStackTrace()) {
            message = message + "\n" + element.toString();
        }
        Logger logger = LoggerFactory.getLogger(source.getClass());
        logger.error(message);
    }

    public static void outLog(Logger logger, EnumLogLevel level, LogInfo log) {
        String json = JSON.toJSONString(log);
        switch (level) {
            case ERROR -> logger.error(json);
            case WARN -> logger.warn(json);
            case DEBUG -> logger.debug(json);
            case TRACE -> logger.trace(json);
            default -> logger.info(json);
        }
    }
}
